import QtQuick 2.0
import QtQuick.Controls 2.5
import PluginHandler 1.0
import FileDialogHandler 1.0

Component {
    Rectangle {
        id: rect
        anchors.fill: parent
        border.color: "#cdcdcd"


        Image {
            width: 32
            height: 32
            anchors.fill: parent
            fillMode: Image.PreserveAspectFit
            anchors.centerIn: parent
            source: "../images/add.png"
        }

        MouseArea {
            id: btb_add_rect_ma
            property color oldColor;
            property Component fileDialogPage;
            anchors.fill: parent
            enabled: true
            hoverEnabled: true

            onEntered: {
                oldColor = rect.color
                rect.color = "#F2F2F2"
                cursorShape = Qt.PointingHandCursor
            }
            onExited: {
                rect.color = oldColor
                if (cursorShape === Qt.PointingHandCursor)
                    cursorShape = Qt.ArrowCursor
            }

            function finishCreation() {
                if (fileDialogPage.status === Component.Ready)
                {
                    console.log("Async Create Component Ready")
                    loader.sourceComponent = fileDialogPage;
                }
            }

            onClicked: {
                cursorShape = Qt.WaitCursor
                fileDialogPage = Qt.createComponent("FileDialogPage.qml", Component.Asynchronous);
                if (fileDialogPage.status === Component.Ready) {
                    finishCreation();
                } else {
                    console.log("Async Create Component Not Ready")
                    fileDialogPage.statusChanged.connect(finishCreation);
                }
            }
        }

        Loader {
            id: loader

            onSourceComponentChanged: {
                btb_add_rect_ma.cursorShape = Qt.ArrowCursor
            }

            Connections {
                target: FileDialogHandler
                function onFileDialogPageCancle() {
                    loader.sourceComponent.destroy();
                }
            }
        }
    }
}



